package com.zfzs.post.modules.agent.service.impl;

import com.zfzs.post.modules.agent.domain.AgentBank;
import com.zfzs.post.modules.agent.repository.AgentBankRepository;
import com.zfzs.post.modules.agent.service.AgentBankService;
import com.zfzs.post.modules.agent.service.dto.AgentBankDTO;
import com.zfzs.post.modules.agent.service.dto.AgentBankQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.AgentBankMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Optional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
* @author ck
* @date 2019-09-05
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentBankServiceImpl implements AgentBankService {

    @Autowired
    private AgentBankRepository agentBankRepository;

    @Autowired
    private AgentBankMapper agentBankMapper;

    @Override
    public Object queryAll(AgentBankQueryCriteria criteria, Pageable pageable){
        Page<AgentBank> page = agentBankRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agentBankMapper::toDto));
    }

    @Override
    public Object queryAll(AgentBankQueryCriteria criteria){
        return agentBankRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
    }

    @Override
    public AgentBank findById(Long id) {
        Optional<AgentBank> agentBank = agentBankRepository.findById(id);
        ValidationUtil.isNull(agentBank,"AgentBank","id",id);
        return agentBank.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentBank create(AgentBank resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setBankId(snowflake.nextId());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return agentBankRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgentBank resources) {
        Optional<AgentBank> optionalAgentBank = agentBankRepository.findById(resources.getBankId());
        ValidationUtil.isNull( optionalAgentBank,"AgentBank","id",resources.getBankId());
        AgentBank agentBank = optionalAgentBank.get();
        agentBank.copy(resources);
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        agentBankRepository.save(agentBank);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        agentBankRepository.deleteById(id);
    }
}